<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018/7/3
 * Time: 10:47
 */
namespace app\admin\controller;
use app\admin\model\Auths;
use app\admin\model\Builder;
use app\admin\model\Commons;
use app\admin\model\Databases;
use app\admin\model\GetHtml;
use app\admin\model\Qinu;
use think\Controller;
use think\Db;
use think\Request;

class Document extends Controller
{
    /**加载文档列表
     * @param Request $request
     * @return \think\response\View
     */
    public function show_list(Request $request){
        $cid = $request->param('cid',0);
        $this->assign('cid',$cid);
//        return $cid;
        $modelid = $request->param('modelid');
        $tiaojian = $request->param('tiaojian',null);
        if(!$modelid){
            $modelid = (Db::name('column')->where('id',$cid)->find())['model'];
        }
        $tiaojians = $tiaojian;
        if($tiaojian=='已启用'){
            $tiaojians = 1;
        }elseif ($tiaojian=='未启用'){
            $tiaojians = 0;
        }
        $model = Db::name('model')->where('id',$modelid)->find();
        if($cid==0){
            if($model['model_type']=='普通模型'){
                if($tiaojian){
                    $data = Db::name('document')->where('id|title|dir|status','like',"%$tiaojians%")->order('dir,id asc')->paginate(10)->toArray();
                }else{
                    $data = Db::name('document')->order('dir,id desc')->paginate(10)->toArray();
                }
            }elseif ($model['model_type']=='单表模型'){
                if($tiaojian){
                    $data = Db::name($model['surface'])->where('id|title|dir|status','like',"%$tiaojians%")->order('dir,id desc')->paginate(10)->toArray();
                }else{
                    $data = Db::name($model['surface'])->order('dir,id desc')->paginate(10)->toArray();
                }
            }
        }else{
            if($model['model_type']=='普通模型'){
                if($tiaojian){
                    $data = Db::name('document')->where('cid',$cid)->where('id|title|dir|status','like',"%$tiaojians%")->order('dir,model_id desc,id desc')->paginate(10)->toArray();
                }else{
                    $data = Db::name('document')->where('cid',$cid)->order('dir,model_id desc,id desc')->paginate(10)->toArray();
                }
            }elseif ($model['model_type']=='单表模型'){
                if($tiaojian){
                    $data = Db::name($model['surface'])->where('cid',$cid)->where('cid',$cid)->where('id|title|dir|status','like',"%$tiaojians%")->order('dir,model_id desc,id desc')->paginate(10)->toArray();
                }else{
                    $data = Db::name($model['surface'])->where('cid',$cid)->order('dir,model_id desc,id desc')->paginate(10)->toArray();
                }
            }

        }
//return $cid;
//        echo '<pre>';print_r($data);exit;
        $this->assign('data',$data['total']);
        $rus = '';
        if($cid!=0){
            $rus = (Db::name('column')->where('id',$cid)->find())['model'];
        }
//         echo '<pre>';print_r($data);exit;
        $data = $data['data'];
        foreach ($data as $key=>$value){
            $arrays = Db::name('model')->where('id',$value['model_id'])->find();
            if($arrays){
                $data[$key]['model']=  $arrays['model_name'].'['.$value['model_id'].']';
            }else{
                $data[$key]['model'] = '';
            }
//            echo '<pre>';exit;
            if($cid!=0){
                $array = Db::name('column')->where('id',$cid)->find();
            }else{
                $array = Db::name('column')->where('id',$value['cid'])->find();
            }

            if($array){
                $data[$key]['cid_name'] = $array['name'];
            }else{
                $data[$key]['cid_name'] = '';
            }
        }
//        echo '<pre>';print_r($data);exit;
        $str = Builder::make('table')
            ->addHead(['button alldel','button add'],['批量删除','添加'],['/admin/document/del','/admin/document/show_add?cid='.$cid.'&modelid='.$rus],['',''],['',''])
            ->addColumn('INDEX','checkbox','<input type="checkbox" onchange="quanxuan(this)"/>','','text-align: center')
            ->addColumn('id','text','ID','','text-align: center')
            ->addColumn('title','text','名称','','text-align: center')
            ->addColumn('cid_name','text','所属栏目','','text-align: center')
            ->addColumn('model','text','模型','','text-align: center')
            ->addColumn('dir','text','描述','','text-align: center')
            ->addColumn('sort','text','排序','','text-align: center')
            ->addColumn('status','status','状态','','text-align: center')
            ->addRight('操作',['icon edit','icon del'],['/admin/document/show_edit?cid='.$cid.'&id=','/admin/document/del?cid='.$cid],['编辑','删除'],'','text-align: center')
            ->addData($data)
            ->addPage('test')
            ->fetch();
        $this->assign('tiaojian',$tiaojian);
        $this->assign('modelid',$modelid);
        $this->assign('str',$str);
        $this->assign('lanmu',0);
       return  $this->fetch('public/list');
    }

    /**加载选择模型
     * @param Request $request
     * @return \think\response\View
     */
    public function show_add(Request $request){
        $cid = $request->param('cid',1);
        $modelid = $request->param('modelid',null);
//        echo $modelid;exit;
        if($modelid&&$cid){
//            return 1;
            return $this->redirect('document/add_desc',['cid'=>$cid,'modelid'=>$modelid]);
        }
        $model = Db::name('model')->where('model_type','普通模型')->select();
        $data = [];
        foreach ($model as $value){
            $data[$value['id']] = $value['model_name'];
        }
        $str =  Builder::make('form','/admin/document/add_desc','','')
            ->addNone('cid',$cid)
            ->addSelect('modelid','选择模型',1,$data)
            ->addButton(['提交','返回'],['submit','back'])
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('public/bianji');
    }

    /**加载添加页面
     * @param Request $request
     * @return mixed|\think\response\Redirect
     */
    public function add_desc(Request $request){
        $modelid = $request->param('modelid',null);
        $cid     = $request->param('cid',1);
        if(!$modelid){
            return redirect('document/show_list?cid='.$cid);
        }
//        echo '<pre>';print_r(Auths::login_auth($modelid));exit;
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        $feild = Db::name('table_field')->where('modelid',$modelid)->order('id asc')->select();
        $model = Db::name('model')->where('id',$modelid)->find();
        $model = $model['model_name'];
        $cidname = Db::name('column')->where('id',$cid)->find();
        $cidname = $cidname['name'];
        $str =  Builder::make('form','/admin/document/add','','')
            ->addNone('cid',$cid)
            ->addNone('model_id',$modelid)
            ->addInput('','所属栏目',$cidname,'','width:50%',true)
            ->addInput('','模型',$model,'','width:50%',true)
            ->addInput('title','名称','','','width:50%')
            ->addInput('dir','描述','','','width:50%')
            ->addInput('sort','排序',100,'','width:50%')
            ->addInput('keywords','关键字','','','width:50%')
            ->addInput('description','关键字描述','','','width:50%')
            ->addSelect('status','是否开启',1,[0=>'关闭',1=>'开启']);
//        echo '<pre>';print_r($feild);exit;
        foreach ($feild as $v){
            if($v['field_type']=='单行文本'){
                $str = $str->addInput($v['field_title'],$v['field_name'],'','','width:50%');
            }elseif ($v['field_type']=='多行文本'){
                $str = $str->addTextarea($v['field_title'],$v['field_name'],'','','width:50%');
            }elseif ($v['field_type']=='单图片'){
                $str = $str->addImg($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='多图片'){
                $str = $str->addImgs($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='单文件'){
                $str = $str->addFile($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='多文件'){
                $str = $str->addFiles($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='单选框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addRadio($v['field_title'],$v['field_name'],'',$v['type_option'],'','',true);
            }elseif ($v['field_type']=='复选框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addCheckbox($v['field_title'],$v['field_name'],'',$v['type_option'],'','',true);
            }elseif ($v['field_type']=='下拉框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addSelect($v['field_title'],$v['field_name'],'',$v['type_option'],'','',true);
            }elseif ($v['field_type']=='联动下拉框'){
                $ss = Db::name($v['tables'])->where('pid',0)->select();
                $str = $str->addLinkageSelect($v['field_title'],$v['field_name'],$v['ci'],$v['tables'],$ss);
            }elseif ($v['field_type']=='编辑文本'){
                $str = $str->addEditor($v['field_title'],$v['field_name'],'');
            }elseif ($v['field_type']=='时间'){
                $str = $str->addInput($v['field_title'],$v['field_name'],'');
            }
        }
            $str = $str->addButton(['提交','返回'],['submit','back'])
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('public/bianji');
    }

    /**添加文档
     * @param Request $request
     * @return array|int|string|\think\response\Redirect
     */
    public function add(Request $request){
        $cid = $request->param('cid',null);
        $model_id = $request->param('model_id',1);//获取模型id
        $model = Db::name('model')->where('id',$model_id)->find();//讲模型id下传
        if(!Auths::login_auth($model_id)){
            return '你没有权限';
        }
        $data = $request->post();
        $document['model_id'] = $model_id;
        $document['title'] = $data['title'];
        $document['cid']   = $cid;
        $document['status']   = $data['status'];
        $document['dir']   = $data['dir'];
        $document['sort']   = $data['sort'];
        $document['keywords']   = $data['keywords'];
        $document['description']   = $data['description'];
        $document['create_time']   = time();
        $document['update_time']   = time();
        $feild = Db::name('table_field')->where('modelid',$model_id)->select();
        $feilds = [];
        if($feild){
            foreach ($feild as $ke =>$va){
                $feilds[$va['field_title']] = $va['field_type'];
            }
        }
        $child = [];
        $file = $request->file();
        foreach ($feilds as $key=>$value){
            if($value=='单行文本'||$value=='多行文本'||$value=='单选框'||$value=='下拉框'||$value=='编辑文本'||$value=='联动下拉框'){
                if(isset($data[$key])){
                    $child[$key] = $data[$key];
                }
            }elseif ($value=='单图片'){
                if(isset($data[$key])){
                    $child[$key] = $data[$key];
                }
            }elseif ($value=='单文件'){
                if(isset($file[$key])){
                    $fil = Qinu::Upfile($file[$key]);
                    $name = explode('/',$fil);
                    $name = end($name);
                    $id = Db::name('enclosure')
                        ->insertGetId([
                            'name'=>$name,
                            'path'=>$fil
                        ]);
                    $child[$key] = $id;
                }
            } elseif ($value=='多图片'){
                if(isset($data[$key])){
                    $child[$key] = trim($data[$key],',');
                }
            }elseif ($value=='多文件'){
                if(isset($file[$key])){
                    $arrays = Qinu::Upfile($file[$key]);
                    $sts = [];
                    if($arrays){
                        foreach ($arrays as $ss){
                            $name = explode('/',$ss);
                            $name = end($name);
                            $id = Db::name('enclosure')
                                ->insertGetId([
                                    'name'=>$name,
                                    'path'=>$ss
                                ]);
                            $sts[] = $id;
                        }
                        $child[$key] = json_encode($sts);
                    }
                }
            }elseif($value=='复选框'){
                if(isset($data[$key])){
                    $child[$key] = json_encode($data[$key]);
                }
            }elseif($value=='时间'){
                if(isset($data[$key])){
                    $child[$key] = strtotime($data[$key]);
                }
            }
        }
        if($model['model_type']=='单表模型'){
            $document = array_merge($document,$child);
            if(Db::name($model['surface'])->insert($document)){
                return $this->fetch('public/error',['code'=>200,'action'=>'/admin/document/show_list?cid='.$cid]);
            }else{
                return $this->fetch('public/error',['code'=>203,'action'=>'/admin/document/show_list?cid='.$cid]);
            }
        }
        $document_id = Db::name('document')->insertGetId($document);
        if(!$document_id){
            $this->fetch('public/error',['code'=>203,'action'=>'/admin/document/show_list?cid='.$cid]);
        }
        $child['uid'] =  $document_id;
        if(Db::name($model['surface'])->insert($child)){
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/document/show_list?cid='.$cid]);
        }else{
            return $this->fetch('public/error',['code'=>203,'action'=>'/admin/document/show_list?cid='.$cid]);
        }
    }
//加载编辑数据
    public function show_edit(Request $request){
        $id = $request->param('id',null);
        $cid = $request->param('cid',0);
        $lanmu = Db::name('column')->where('id',$cid)->find();
        $model = Db::name('model')->where('id',$lanmu['model'])->find();
        if($model['model_type']=='单表模型'){
            $document = Db::name($model['surface'])->where('id',$id)->find();
        }elseif ($model['model_type']=='普通模型'){
            $document = Db::name('document')->where('id',$id)->find();
        }

        if(!Auths::login_auth($model['id'])){
            return '你没有权限';
        }
        $cidname = (Db::name('column')->where('id',$document['cid'])->find())['name'];
        $feild = Db::name('table_field')->where('modelid',$model['id'])->order('id asc')->select();
//        $model = Db::name('model')->where('id',$document['model_id'])->find();
        if($model['model_type']=='单表模型'){
            $child = $document;
        }elseif ($model['model_type']=='普通模型'){
            $child = Db::name($model['surface'])->where('uid',$id)->find();
        }
//        $array = array_merge($document,$child);
        $str =  Builder::make('form','/admin/document/update?cid='.$cid,'','')
            ->addNone('id',$id)
            ->addNone('cid',$cid)
            ->addNone('model_id',$document['model_id'])
            ->addInput('','所属栏目',$cidname,'','width:50%',true)
            ->addInput('','模型',$model['model_name'],'','width:50%',true)
            ->addInput('title','名称',$document['title'],'','width:50%')
            ->addInput('dir','描述',$document['dir'],'','width:50%')
            ->addInput('sort','排序',$document['sort'],'','width:50%')
            ->addInput('keywords','关键字',$document['keywords'],'','width:50%')
            ->addInput('description','关键字描述',$document['description'],'','width:50%')
            ->addSelect('status','是否开启',$document['status'],[0=>'关闭',1=>'开启']);
        foreach ($feild as $v){
//            echo '<pre>';print_r($v['field_type']);exit;
            if($v['field_type']=='单行文本'){
                $str = $str->addInput($v['field_title'],$v['field_name'],$child[$v['field_title']],'','width:50%');
            }elseif ($v['field_type']=='多行文本'){
                $str = $str->addTextarea($v['field_title'],$v['field_name'],$child[$v['field_title']],'','width:50%');
            }elseif ($v['field_type']=='单图片'){
                if($child[$v['field_title']]){
                    $str = $str->addImg($v['field_title'],$v['field_name'],$child[$v['field_title']]);
                }else{
                    $str = $str->addImg($v['field_title'],$v['field_name']);
                }
            }elseif ($v['field_type']=='多图片'){
                if($child[$v['field_title']]){
                    $str = $str->addImgs($v['field_title'],$v['field_name'],$child[$v['field_title']]);
                }else{
                    $str = $str->addImgs($v['field_title'],$v['field_name']);
                }
            }elseif ($v['field_type']=='单文件'){
                $str = $str->addFile($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='多文件'){
                $str = $str->addFiles($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='单选框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addRadio($v['field_title'],$v['field_name'],$child[$v['field_title']],$v['type_option'],'','',true);
            }elseif ($v['field_type']=='复选框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,]+/', trim($v['type_option'], ",;\r\n"));
                }
                if($child[$v['field_title']]){
                    $val = json_decode($child[$v['field_title']],true);
                    $str = $str->addCheckbox($v['field_title'],$v['field_name'],$val,$v['type_option'],'','',true);
                }else{
                    $str = $str->addCheckbox($v['field_title'],$v['field_name'],'',$v['type_option'],'','',true);
                }
            }elseif ($v['field_type']=='下拉框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addSelect($v['field_title'],$v['field_name'],$child[$v['field_title']],$v['type_option'],'','',true);
            }elseif ($v['field_type']=='联动下拉框'){
//                echo '<pre>';print_r($v);exit;
                $options = Db::name($v['tables'])->where('pid',0)->select();
                $str = $str->addLinkageSelect($v['field_title'],$v['field_name'].'[修改必须重新选择]',$v['ci'],$v['tables'],$options,'','width:50%',$child[$v['field_title']]);
            }elseif ($v['field_type']=='编辑文本'){
                $str = $str->addEditor($v['field_title'],$v['field_name'],$child[$v['field_title']]);
            }elseif ($v['field_type']=='时间'){
                $str = $str->addInput($v['field_title'],$v['field_name'],date('Y-m-d H:i:s',$child[$v['field_title']]));
            }
        }
        $str = $str->addButton(['提交','返回'],['submit','back'])
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('public/bianji');
    }
    public function update(Request $request){
        $data = $request->post();
//        echo '<pre>';print_r($data);exit;
        $document['id']   = $data['id'];
        $document['title'] = $data['title'];
        $document['status']   = $data['status'];
        $document['dir']   = $data['dir'];
        $document['sort']   = $data['sort'];
        $document['keywords']   = $data['keywords'];
        $document['description']   = $data['description'];
        $document['update_time']   = time();
        $model = Db::name('model')->where('id',$data['model_id'])->find();
        if(!Auths::login_auth($data['model_id'])){
            return '你没有权限';
        }
        $feild = Db::name('table_field')->where('modelid',$data['model_id'])->select();
//        echo '<pre>';print_r($feild);exit;
        if($model['model_type']=='普通模型'){
            $lao = Db::name($model['surface'])->where('uid',$data['id'])->find();
        }elseif ($model['model_type']=='单表模型'){
            $lao = Db::name($model['surface'])->where('id',$data['id'])->find();
        }
        $feilds = [];
        if($feild){
            foreach ($feild as $ke =>$va){
                $feilds[$va['field_title']] = $va['field_type'];
            }
        }
        $child = [];
        $file = $request->file();
        foreach ($feilds as $key=>$value){
            if($value=='单行文本'||$value=='多行文本'||$value=='单选框'||$value=='下拉框'||$value=='编辑文本'||$value=='联动下拉框'){
                if(isset($data[$key])){
                    $child[$key] = $data[$key];
                }
            }elseif ($value=='单图片'){
                if(isset($data[$key])){
                    $child[$key] =  $data[$key];
                }
            }elseif ($value=='单文件'){
//                return $file[$key];
                if(isset($file[$key])){
                    $fil = Qinu::Upfile($file[$key]);
                    $name = explode('/',$fil);
                    $name = end($name);
                    $child[$key] = Db::name('enclosure')->insertGetId([
                        'name'=>$name,
                        'path'=>$fil
                    ]);
                    if($lao[$key]){
                        $path = (Db::name('enclosure')->where('id',$lao[$key])->find())['path'];
                        Db::name('enclosure')->where('id',$lao[$key])->delete();
                        Qinu::DeleFile($path);
                    }
                }
            } elseif ($value=='多图片'){
                if(isset($data[$key])){
                    $child[$key] =  trim($data[$key],',');
                }
            }elseif ($value=='多文件'){
//                echo '<pre>';print_r($file[$key]);exit;
                if(isset($file[$key])){
                    $arrays = Qinu::Upfile($file[$key]);
                    if($arrays){
                        $sts = [];
                        foreach ($arrays as $ss){
                            $name = explode('/',$ss);
                            $name = end($name);
                            $sts[] = Db::name('enclosure')
                                ->insertGetId([
                                    'name'=>$name,
                                    'path'=>$ss
                                ]);
                        }
                        $child[$key] = json_encode($sts);
                        if($lao[$key]){
                            $ar = json_decode($lao[$key],true);
                            foreach ($ar as $v){
                                $path = (Db::name('enclosure')
                                    ->where('id',$v)
                                    ->find())['path'];
                                Db::name('enclosure')
                                    ->where('id',$v)
                                    ->delete();
                                Qinu::DeleFile($path);
                            }
                        }
                    }
                }
            }elseif($value=='复选框'){
                if(isset($data[$key])){
                    $child[$key] = json_encode($data[$key]);
                }
            }elseif($value=='时间'){
                if(isset($data[$key])){
                    $child[$key] = strtotime($data[$key]);
                }
            }
        }
        if($model['model_type']=='普通模型'){
            $bool = Db::name('document')->where('id',$data['id'])->update($document);
            $bools = Db::name($model['surface'])->where('uid',$data['id'])->update($child);
        }elseif ($model['model_type']=='单表模型'){
            $document = array_merge($document,$child);
            $bool = Db::name($model['surface'])->where('id',$data['id'])->update($document);
            $bools = $bool;
        }
        if($bool||$bools){
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/document/show_list?cid='.$data['cid']]);
        }else{
           return  $this->fetch('public/error',['code'=>203,'action'=>'/admin/document/show_list?cid='.$data['cid']]);
        }
    }
    /**编辑文档
     * @param Request $request
     * @return string|\think\response\Redirect
     */
    public function edit_p(Request $request){
        $id = $request->param('id',null);
        $pid = $request->param('pid',null);
        if(!$id){
            return '请输入修改的文档id';
        }
        $modelid = (Db::name('document')->where('id',$id)->find())['model'];
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
//        return $pid;
//        $str = Databases::get_edit($id,$pid);
        $str = GetHtml::GetAllHtml($pid,$id);
//        echo '<pre>';print_r($str);exit;
        return view('bianji',['uid'=>$id,'str'=>$str]);
    }

    /**分页
     * @param Request $request
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function page(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $id = $request->param('id',null);
        if($tiaojian){
            if($id){
                $data = (new Commons())->getData($id,'document','id','pid',$tiaojian);
//                $datarows = count($data);

                $data = array_slice($data,($page-1)*$limit,$limit);
            }else{
                $data = Db::view('document',true)
                    ->view('model','model_name','document.model=model.id','left')
                    ->where('model_name|id','like',"%$tiaojian%")
                    ->order('document.id desc')
                    ->limit(($page-1)*$limit,$limit)
                    ->select();
            }

        }else{
            if($id){
                $data = (new Commons())->getData($id);
//                $datarows = count($data);
                $data = array_slice($data,($page-1)*$limit,$limit);

            }else{
                $data = $data = Db::view('document',true)
                    ->view('model','model_name','document.model=model.id','left')
                    ->order('document.id desc')
                    ->limit(($page-1)*$limit,$limit)
                    ->select();
            }
        }
        if($data){
            foreach ($data as $key=>$value){
                if($value['pid']==0){
                    $data[$key]['pidname'] = '最上级';
                }else{
                    $data[$key]['pidname'] = (Db::name('document')->where('id',$value['pid'])->find())['title'];
                }
            }
        }
        return $data;
    }
public function pages(Request $request){
        $cid = $request->param('cid',0);
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $tiaojian = $request->param('tiaojian',null);
        $lanmu = Db::name('column')->where('id',$cid)->find();
        $model = Db::name('model')->where('id',$lanmu['model'])->find();
        if($tiaojian=='已启用'){
            $tiaojian = 1;
        }elseif ($tiaojian=='未启用'){
            $tiaojian = 0;
        }
        if($cid==0){
            if($model['model_type']=='普通模型'){
                if($tiaojian){
                    $data = Db::name('document')
                        ->where('id|title|dir|status','like',"%$tiaojian%")
                        ->order('dir,model_id desc,id desc')
                        ->limit(($page-1)*$limit,$limit)
                        ->select();
                }else{
                    $data = Db::name('document')
                        ->order('dir,model_id desc,id desc')
                        ->limit(($page-1)*$limit,$limit)
                        ->select();
                }
            }elseif($model['model_type']=='单表模型'){
                if($tiaojian){
                    $data = Db::name($model['surface'])
                        ->where('id|title|dir|status','like',"%$tiaojian%")
                        ->order('dir,model_id desc,id desc')
                        ->limit(($page-1)*$limit,$limit)
                        ->select();
                }else{
                    $data = Db::name($model['surface'])
                        ->order('dir,model_id desc,id desc')
                        ->limit(($page-1)*$limit,$limit)
                        ->select();
                }
            }
        }else{
            if($model['model_type']=='普通模型'){
                if($tiaojian){
                    $data = Db::name('document')
                        ->where('id|title|dir|status','like',"%$tiaojian%")
                        ->where('cid',$cid)
                        ->order('dir,model_id desc,id desc')
                        ->limit(($page-1)*$limit,$limit)
                        ->select();
                }else{
                    $data = Db::name('document')
                        ->where('cid',$cid)
                        ->order('dir,model_id desc,id desc')
                        ->limit(($page-1)*$limit,$limit)
                        ->select();
                }
            }elseif($model['model_type']=='单表模型'){
                if($tiaojian){
                    $data = Db::name($model['surface'])
                        ->where('id|title|dir|status','like',"%$tiaojian%")
                        ->where('cid',$cid)
                        ->order('dir,model_id desc,id desc')
                        ->limit(($page-1)*$limit,$limit)
                        ->select();
                }else{
                    $data = Db::name($model['surface'])
                        ->where('cid',$cid)
                        ->order('dir,model_id desc,id desc')
                        ->limit(($page-1)*$limit,$limit)
                        ->select();
                }
            }
        }

        foreach ($data as $key=>$value){
            $data[$key]['cid'] = (Db::name('column')->where('id',$value['cid'])->find())['name'];
            $model = Db::name('model')->where('id',$value['model_id'])->find();
            $data[$key]['model_id'] = $model['model_name'].'['.$model['id'].']';
        }
        return json(['code'=>200,'data'=>$data]);
}
    /**
     * @return int|string
     */
    public function del(Request $request){
        $id = $request->param('id');
        $cid = $request->param('cid',0);
        $lanmu = Db::name('column')->where('id',$cid)->find();
        $model = Db::name('model')->where('id',$lanmu['model'])->find();
        if($model['model_type']=='单表模型'){
            $document = Db::name($model['surface'])->where('id','in',$id)->select();
        }elseif ($model['model_type']=='普通模型'){
            $document = Db::name('document')->where('id','in',$id)->select();
        }
//        return json(['code'=>200,'msg'=>'删除失败']);
        foreach ($document as $value){
//            $model = (Db::name('model')->where('id',$value['model_id'])->find())['surface'];
            if(!Auths::login_auth($value['model_id'])){
                return json(['code'=>200,'msg'=>'你没有权限']);
            }
            if($model['model_type']=='普通模型'){
                $data = Db::name($model['surface'])->where('uid',$value['id'])->find();
            }elseif ($model['model_type']=='单表模型'){
                $data = Db::name($model['surface'])->where('id',$value['id'])->find();
            }

//            return json(['code'=>200,'msg'=>$value['id']]);
            $feild = Db::name('table_field')->where('modelid',$value['model_id'])->select();
            foreach ($feild as $va){
                if($va['field_type']=='单图片'){
                    if($data[$va['field_title']]){
                        $name = (Db::name('enclosure')->where('id',$data[$va['field_title']])->find())['path'];
                        Qinu::DeleFile($name);
                        Db::name('enclosure')->where('id',$data[$va['field_title']])->delete();
                    }
                }
                if($va['field_type']=='单文件'){
                    if($data[$va['field_title']]){
                        Qinu::DeleFile($data[$va['field_title']]);
                    }
                }
                if($va['field_type']=='多图片'){
                    if($data[$va['field_title']]){
                        $array = explode(',',trim($data[$va['field_title']],','));
                        foreach ($array as $s){
                            $name = (Db::name('enclosure')->where('id',$s)->find())['path'];
                            Qinu::DeleFile($name);
                            Db::name('enclosure')->where('id',$data[$va['field_title']])->delete();
                        }
                    }
                }
                if($va['field_type']=='多文件'){
                    if($data[$va['field_title']]){
                        $array = json_decode($data[$va['field_title']],true);
                        foreach ($array as $v){
                            Qinu::DeleFile($v);
                        }

                    }
                }
            }
            if($model['model_type']=='普通模型'){
                Db::name($model['surface'])->where('uid',$value['id'])->delete();
            }
        }
        if($model['model_type']=='普通模型'){
            if(Db::name('document')->where('id','in',$id)->delete()){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>200,'msg'=>'删除失败']);
            }
        }elseif ($model['model_type']=='单表模型'){
            if(Db::name($model['surface'])->where('id','in',$id)->delete()){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>200,'msg'=>'删除失败']);
            }
        }

    }

    /**成功或失败的跳转
     * @param Request $request
     * @return \think\response\View
     */
    public function err(Request $request){
        $str = urldecode($request->param('str'));
      $code = $request->param('code');
      //echo '<pre>';
      //print_r($str);exit;
      //echo urldecode($str);exit;
        return view('err',['str'=>$str,'code'=>$code]);
    }

    /**更新普通模型
     * @param Request $request
     * @return int
     */
    public function update_p(Request $request){
        $data = $request->post();
        $file = $request->file();
//        echo 'nihao';exit;
        $bool = Databases::update_p($data,$file);
      if($bool){
        $str = ['str'=>"更新成功",'code'=>200];
      }else{
        $str = ['str'=>"更新失败",'code'=>201];
      }
      return redirect('err',$str);
    }

    public function upfiles(Request $request){
        $file = $request->file();
//        $files = $request->post();
        $file = Qinu::Upfile($file);
        $data = [];
        foreach ($file as $key=>$value){
            $name = explode('/',$value);
            $name = end($name);
            $data[$key]['dir'] = $value;
            $data[$key]['id']=Db::name('enclosure')->insertGetId([
                'name' =>$name,
                'path' =>$value
            ]);
        }
        return json($data);
    }
    public function del_img(Request $request){
        $id = $request->param('id');
        $data = Db::name('enclosure')->where('id',$id)->find();
//        return $id;
        if(Qinu::DeleFile($data['path'])){
            if(Db::name('enclosure')->where('id',$id)->delete()){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>203,'msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }
    public function liandong(Request $request){
        $id = $request->param('id');
        $modelid = $request->param('modelid');
        $model = (Db::name('model')->where('id',$modelid)->find())['surface'];
        $data = Db::name($model)->where('pid',$id)->select();
        return json(['code'=>200,'data'=>$data]);
    }
}